﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VideoLib.Models;
using Application = System.Windows.Application;
using Control = System.Windows.Controls.Control;
using ListBox = System.Windows.Controls.ListBox;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
using Point = System.Windows.Point;

namespace VideoLib.Views
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : UserControl
    {
        public MainView()
        {
            InitializeComponent();
        }

        bool isListNavigate = false;
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            isListNavigate = true;
            lstb.ScrollIntoView((sender as ListBox).SelectedItem);
            isListNavigate = false;
        }

        private void GridSplitter_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            cateList.Width = MainGrid.ColumnDefinitions[0].Width.Value;
            MainGrid.ColumnDefinitions[0].Width = GridLength.Auto;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            cateList.Width = MainGrid.ColumnDefinitions[0].Width.Value;
            MainGrid.ColumnDefinitions[0].Width = GridLength.Auto;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var folder = (cateList.SelectedItem as FolderInfo).Direction;
            System.Diagnostics.Process.Start("explorer.exe", folder);
        }

        // 在上级控件中查找指定类型的控件
        public static T FindParent<T>(DependencyObject child) where T : DependencyObject
        {
            // 获取当前控件的父级
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);

            // 如果到达了视图的顶部，没有找到父级，则返回 null
            if (parentObject == null) return null;

            // 如果父级是我们要找的类型，则直接返回
            T parent = parentObject as T;
            if (parent != null)
            {
                return parent;
            }
            else
            {
                // 否则，递归地在上一级父级中查找
                return FindParent<T>(parentObject);
            }
        }

        private void ListBoxItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
        {
            e.Handled = !isListNavigate;
        }
    }
}
